package com.lexsoft.kanaiscube;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lexsoft.kanaiscube.util.Tools;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class DataList extends ListFragment {

	private SimpleAdapter sa;
	private String[] datalist;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
		int count = Integer.parseInt(getString(R.string.data_items_count));
		datalist = new String[count];
		try{
			for(int i=1;i<=count;i++){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("list_item_text", getString(Tools.getResourceIdByName("string","data_item_"+i)));
				map.put("list_item_image", R.drawable.item);
				datalist[i-1]=getString(Tools.getResourceIdByName("string","data_item_class_"+i));
				datas.add(map);
			}
		}catch(Exception e){
		}
		sa = new SimpleAdapter(getActivity(),datas,R.layout.list_item_layout,new String[]{"list_item_image","list_item_text"},new int[]{R.id.list_item_image,R.id.list_item_text});
		setListAdapter(sa);

	}


	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if(position<datalist.length&&datalist[position]!=null&&!"".equals(datalist[position])){
			Fragment fragment = Tools.loadFragment(datalist[position]);
			Bundle args = new Bundle();
			fragment.setArguments(args);
			FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
			ft.replace(R.id.container, fragment);
			ft.addToBackStack(null);
			ft.commit();
			getActivity().findViewById(R.id.container).setBackgroundResource(R.drawable.detail);
		}
//		super.onListItemClick(l, v, position, id);
	}
}
